package cn.itsource.ymcc.producer.listener;

import cn.itsource.ymcc.constant.Constants;
import cn.itsource.ymcc.constant.OrderConstants;
import cn.itsource.ymcc.domain.CourseOrder;
import cn.itsource.ymcc.domain.CourseOrderItem;
import cn.itsource.ymcc.mapper.CourseOrderMapper;
import cn.itsource.ymcc.service.ICourseOrderItemService;
import cn.itsource.ymcc.service.ICourseOrderService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionListener;
import org.apache.rocketmq.spring.core.RocketMQLocalTransactionState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//保存课程订单以及支付订单的事务消息
@Component
@Slf4j
@RocketMQTransactionListener(
        txProducerGroup = OrderConstants.MQ_COURSEORDER_PAY_GROUP_TRANSACTION
)
public class CourseOrderTransactionListener implements RocketMQLocalTransactionListener {

    @Autowired
    private ICourseOrderService courseOrderService;

    @Autowired
    private CourseOrderMapper courseOrderMapper;

    @Autowired
    private ICourseOrderItemService orderItemService;
 	/**
     * 执行本地事务
     *  保存订单和订单明细
     * @param message   事务消息内容，也就是sendMessageInTransaction方法的第三个参数
     * @param args   扩展参数，也就是sendMessageInTransaction方法的第四个参数
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public RocketMQLocalTransactionState executeLocalTransaction(Message message, Object args) {
        log.info("事务监听器 message = {} ",message.getPayload());
        log.info("事务监听器 args = {}",args);

        if(message == null || message.getPayload() == null){
            log.error("事务监听器 拿到的数据有问题 {} ,{}",message ,args );
            return RocketMQLocalTransactionState.ROLLBACK;
        }

        try {
            //执行本地事务
            // 1.获取对象
            Object payload = message.getPayload();
            String s = new String((byte[]) payload, Constants.UTF8);
            CourseOrder courseOrder = JSON.parseObject(s, CourseOrder.class);
            if (courseOrder == null){
                return RocketMQLocalTransactionState.ROLLBACK;
            }
            //2.执行本地事务
            courseOrderMapper.insert(courseOrder);
            List<CourseOrderItem> itemList = courseOrder.getItemList();
            for (CourseOrderItem courseOrderItem : itemList) {
                courseOrderItem.setOrderId(courseOrder.getId());
            }
            orderItemService.saveBatch(itemList);

            return RocketMQLocalTransactionState.COMMIT;
        }catch (Exception e){
            e.printStackTrace();
        }
        log.info("事务消息回滚");
        return RocketMQLocalTransactionState.ROLLBACK;
    }

    //检查本地事务 - 检查订单是否超过
    @Override
    public RocketMQLocalTransactionState checkLocalTransaction(Message message) {

        return RocketMQLocalTransactionState.ROLLBACK;
    }
}